//=============================================================================
// PlasmaGunChargedFire.
//=============================================================================
class PlasmaGunChargedFire extends BioChargedFire; //ProjectileFire;

#exec obj load file=GeneralAmbience.uax

var sound LinkedFireSound;
var string LinkedFireForce;  // jdf
/*
var() float GoopUpRate;
var() int MaxGoopLoad;
var() int GoopLoad;
var() sound HoldSound;*/
var() int GoopStartAmmo;
var() float         mHoldSpeedMin;
var() float         mHoldSpeedMax;
var() float         mHoldSpeedGainPerSec;
var() float         mHoldClampMax;

var Sound ChargingSound;
var xEmitter ChargingEmitter1, ChargingEmitter2;
var bool bAutoRelease;
var float FullyChargedTime, ChargeScale;
var String ChargingForce;
var bool bStartedChargingForce;





function PostBeginPlay()
{
    Super.PostBeginPlay();
    mHoldClampMax = (mHoldSpeedMax - mHoldSpeedMin) / GoopUpRate; //mHoldSpeedGainPerSec;
}

simulated function DestroyEffects()
{
    if (ChargingEmitter1 != None)
    {
	ChargingEmitter1.AmbientSound=None;
        ChargingEmitter1.Destroy();
    }
    if (ChargingEmitter2 != None)
    {
	ChargingEmitter2.AmbientSound=None;
        ChargingEmitter2.Destroy();
    }
	Super.DestroyEffects();
}

function DrawMuzzleFlash(Canvas Canvas)
{
   if (ChargingEmitter1 != None && HoldTime > 0.0 && !bNowWaiting)
    {
		ChargingEmitter1.SetLocation( Weapon(Owner).GetEffectStart() );
        Canvas.DrawActor( ChargingEmitter1, false, false, Weapon(Owner).DisplayFOV );
    }

   if (ChargingEmitter2 != None && HoldTime > 0.0 && !bNowWaiting)
    {
//		ChargingEmitter2.SetDrawScale( 0.6 + GoopLoad / 25);
		ChargingEmitter2.SetLocation( Weapon(Owner).GetEffectStart() );
        Canvas.DrawActor( ChargingEmitter2, false, false, Weapon(Owner).DisplayFOV );
    }

    if (FlashEmitter != None)
    {
        FlashEmitter.SetRotation(Weapon(Owner).Rotation);
        Super.DrawMuzzleFlash(Canvas);
    }

   if ( (ChargingEmitter1 != None && ChargingEmitter1.AmbientSound != None)
		&& ((HoldTime <= 0.0) || bNowWaiting) )
        ChargingEmitter1.AmbientSound = None;

   if ( (ChargingEmitter2 != None && ChargingEmitter2.AmbientSound != None)
		&& ((HoldTime <= 0.0) || bNowWaiting) )
        ChargingEmitter2.AmbientSound = None;
}

function FlashMuzzleFlash()
{
    if (FlashEmitter != None)
    {
            FlashEmitter.Skins[0] = FinalBlend'TFMedia.Plasma_Muz_blueFB';
    }
    Super.FlashMuzzleFlash();
}

function ServerPlayFiring()
{
        FireSound = default.FireSound;
    Super.ServerPlayFiring();
}

//function PlayFiring()
//{
//        FireSound = default.FireSound;
//    Super.PlayFiring();
//}

simulated function ModeTick(float dt)
{
if (FlashEmitter != None)
    {
        FlashEmitter.Skins[0] = FinalBlend'TFMedia.PlasmaMuzProjBlueFB';
    }
}













function InitEffects()
{
    Super.InitEffects();
//    if( FlashEmitter == None )
//        FlashEmitter = Weapon(Owner).FireMode[1].FlashEmitter;

	if ( Level.NetMode != NM_DedicatedServer )
	{
		ChargingEmitter1 = Spawn(class'PlasmaCharge');
		ChargingEmitter1.mRegenPause = true;
		ChargingEmitter1.AmbientSound=None;
		ChargingEmitter2 = Spawn(class'PlasmaChargeTwo');
		ChargingEmitter2.mRegenPause = true;
		ChargingEmitter2.AmbientSound=None;
	}
    //bStartedChargingForce = false;  // jdf
    Super.InitEffects();

}


function ModeHoldFire()
{
    if (Weapon.HasAmmo())
    {
        Super.ModeHoldFire();
        GotoState('Hold');
    }
}

function float MaxRange()
{
	return 1500;
}

simulated function bool AllowFire()
{
    return (Weapon.HasAmmo() || GoopLoad > 0);
}

event ModeDoFire()
{
    Recoil();
    Super.ModeDoFire();
}

simulated function PlayStartHold()
{
    Weapon(Owner).PlayAnim('AltFire', 1.0 / (GoopUpRate*MaxGoopLoad), 0.1);
}

simulated function PlayFiring()
{
	Super.PlayFiring();
	Weapon.OutOfAmmo();
}

state Hold
{
    simulated function BeginState()
    {
        GoopLoad = 0;
	GoopStartAmmo=Weapon.AmmoAmount(1);
        SetTimer(GoopUpRate, true);
        //Weapon.PlayOwnedSound(sound'WeaponSounds.Biorifle_charge',SLOT_Interact,TransientSoundVolume);
        //Weapon.PlayOwnedSound(sound'GeneralAmbience.computerfx31',SLOT_Interact,TransientSoundVolume+3*GoopLoad);
        ClientPlayForceFeedback( "BioRiflePowerUp" );  // jdf
        Timer();
    }

    simulated function Timer()
    {

    local float Regen;
//    local float ChargeScale;


			if ( Weapon.HasAmmo() )
			GoopLoad++;
        Weapon.ConsumeAmmo(0,1);
        if (Weapon.HasAmmo() == False || GoopLoad >= default.MaxGoopLoad)
        {
//          FreezeAnimAt(float(GoopLoad)/float(MaxGoopLoad), 0);		//NEEDS FIXING!!!
            SetTimer(0.0, false);
//	    ModeDoFire();
	    GotoState('');
        }

//     Instigator.AmbientSound = ChargingSound;
//     Instigator.SoundPitch = 64+Regen*2;
//            ChargeScale = FMin(HoldTime, FullyChargedTime);
	    ChargeScale = FMin(GoopLoad/3, 25);
	    Regen = float(GoopLoad);


            if (ChargingEmitter1 != None)
            {
                ChargingEmitter1.mRegenPause = false;
	        ChargingEmitter1.AmbientSound=Sound'WeaponSounds.LinkGun.BLinkGunBeam2';
    		ChargingEmitter1.SoundPitch = 32+Regen*4;
    		ChargingEmitter1.SoundVolume = 128+Regen*4;
            ChargingEmitter1.mRegenRange[0] = 50 + Regen / 5;
            ChargingEmitter1.mRegenRange[1] = 50 + Regen / 5;
            ChargingEmitter1.mSpeedRange[0] = -(15 + Regen * 2);
            ChargingEmitter1.mSpeedRange[1] = -(30 + Regen * 2);
//          Regen = FMax((ChargeScale / 30.0),0.20);
//          ChargingEmitter1.mLifeRange[0] = Regen-(GoopLoad*2);
//          ChargingEmitter1.mLifeRange[1] = Regen-(GoopLoad*2);
            //Regen=0;
	    }

	    if (ChargingEmitter2 != None)
        {
            ChargingEmitter2.mRegenPause = false;
	        ChargingEmitter2.AmbientSound=Sound'GeneralAmbience.computerfx31';
    		ChargingEmitter2.SoundVolume = 64+Regen*4;
    		ChargingEmitter2.SetDrawScale(ChargingEmitter2.Default.DrawScale + Regen / 25);
	    }

    }

    simulated function EndState()
    {
        StopForceFeedback( "BioRiflePowerUp" );  // jdf
    }
}

function projectile SpawnProjectile(Vector Start, Rotator Dir)
{
    local PlasmaGunChargedProjectile Glob;

    GotoState('');

    if (GoopLoad == 0) return None;

    Glob = Spawn(class'PlasmaGunChargedProjectile',,, Start, Dir);
    if ( Glob != None )
    {
		Glob.Damage *= DamageAtten;
		Glob.SetGoopLevel(GoopLoad);
	}
    GoopLoad = 0;
    GoopStartAmmo=0;
    if ( Weapon.HasAmmo() == False )
        Weapon.OutOfAmmo();
    return Glob;

    if (ChargingEmitter1 != None){
        ChargingEmitter1.mRegenPause = true;
	ChargingEmitter1.AmbientSound=None;}
    if (ChargingEmitter2 != None){
        ChargingEmitter2.mRegenPause = true;
	ChargingEmitter1.AmbientSound=None;}

//    Recoil();
}

function StartBerserk()
{
    GoopUpRate = default.GoopUpRate*0.75;
}

function StopBerserk()
{
    GoopUpRate = default.GoopUpRate;
}

function Recoil()
{
    local Rotator NewRotation;
    local float NewPitch, NewYaw;

    if ( (Instigator != None) && Instigator.IsFirstPerson()){
    NewPitch = int(Frand()*300); NewYaw = int(Frand()*500);
    if (Frand() > 0.5) NewYaw *= -1;
    if (Frand() > 0.5) NewPitch *= -1;
    NewRotation = Instigator.GetViewRotation();
    NewRotation.Pitch += NewPitch;
    NewRotation.Yaw += NewYaw;
    Instigator.SetViewRotation(NewRotation);}
}

defaultproperties
{
     GoopUpRate=1.00000
     MaxGoopLoad=25
     mHoldSpeedMin=1.000000
     mHoldSpeedMax=20.000000
     mHoldSpeedGainPerSec=1.000000
     bWaitForRelease=True
     FireSound=SoundGroup'WeaponSounds.RocketLauncher.RocketLauncherFire'
     AmmoClass=Class'Taskforces.PlasmaGunAmmo'
     ProjectileClass=Class'Taskforces.PlasmaGunProjectile'
     FlashEmitterClass=Class'Taskforces.PlasmaMuzFlashProj1st'
}
